<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: LoanRepay.php
// +----------------------------------------------------------------------
// | Author: gxs
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
// | Others: 其他内容的说明
// +----------------------------------------------------------------------
// | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
// +----------------------------------------------------------------------
// | 1.Date: 2019/2/28 Author: gxs Modification: 创建
// +----------------------------------------------------------------------

namespace App\ModelTransfers\WsLoans;

use App\ModelTransfers\Base;

class LoanRepay extends Base
{
    protected $table = 'ws_loan.loan_repay';

    /**
     * Description: 关联申请表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function relationLoanApply()
    {
        return $this->hasOne('App\ModelTransfers\WsLoans\LoanApply', 'id', 'apply_id')->where('is_valid', self::IS_VALID);
    }

    /**
     * Description: 关联扣款记录表,有效 && 成功
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function relationLoanVirement()
    {
        return $this->hasOne('App\ModelTransfers\WsLoans\LoanVirement', 'id', 'virement_id')
            ->where([
                ['is_valid', self::IS_VALID],
                ['virement_status', LoanVirement::VIREMENT_STATUS_TWO],
        ]);
    }

    /**
     * Description: 关联平台管理费扣款记录表
     * Author: Gxs
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function relationLoanOverdueVirement()
    {
        return $this->hasOne('App\ModelTransfers\WsLoans\LoanOverdueVirement', 'repay_id', 'id')
            ->where([
                ['is_valid', self::IS_VALID],
                ['virement_status', LoanVirement::VIREMENT_STATUS_TWO],
            ]);
    }

    /**
     * Description: 获取分期记录本金
     * Author: Gxs
     * @param $value
     * @return int
     */
    public function getActualPrincipalAmountAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }

    /**
     * Description: 获取分期记录计划本金
     * Author: Gxs
     * @param $value
     * @return int
     */
    public function getPlanPrincipalAmountAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }
}